//
//  JUImageScollView.m
//  JUImageGalleryDemo
//
//  Created by Ju on 15/8/20.
//  Copyright (c) 2015年 juwenz. All rights reserved.
//

#import "JUImageScollView.h"

@interface JUImageScollView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation JUImageScollView

#pragma mark - 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self registScrollViewFrameChangedObserver];
        
        self.delegate = self;
        _imageView = [[UIImageView alloc] init];

        [self addSubview:self.imageView];
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image {
    self = [self initWithFrame:frame];
    if(self) {
        if (image) {
            _image = image;
            _imageView.image = image;
            [self resetImage:_imageView.image];
        }
    }
    return self;
}

- (instancetype)initWithImage:(UIImage *)image {
    self = [self initWithFrame:CGRectZero image:image];
    if(self) {
        
    }
    return self;
}

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"frame"];
}

#pragma mark - 私有方法
- (void)registScrollViewFrameChangedObserver {
    [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"frame"]) {
        self.minimumZoomScale = 1.0f;
        self.maximumZoomScale = 1.0f;
        CGRect frame = self.bounds;
        UIImage *image = self.imageView.image;
        
        if (image) {
            if (CGRectEqualToRect(CGRectZero, self.imageView.frame)) {
                CGRect frame = CGRectZero;
                frame.size = image.size;
                self.imageView.frame = frame;
            }
            CGFloat heightScale = frame.size.height/image.size.height;
            CGFloat widthScale = frame.size.width/image.size.width;
            CGFloat minScale = MIN(heightScale, widthScale);
            self.minimumZoomScale = minScale;
            self.maximumZoomScale = 2.0f;
            self.zoomScale = minScale;
        }
    }
}

- (void)registTapGestures
{
    //注册双击事件
    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:doubleTapRecognizer];
    
    //注册拉伸事件
    UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
    twoFingerTapRecognizer.numberOfTapsRequired = 1;
    twoFingerTapRecognizer.numberOfTouchesRequired = 2;
    [self addGestureRecognizer:twoFingerTapRecognizer];
    
    //注册单击事件
    UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSingleTapped:)];
    singleTapRecognizer.numberOfTapsRequired = 1;
    singleTapRecognizer.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:singleTapRecognizer];
    
    [singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
}

- (void)centerScrollViewContents {
    CGSize boundsSize = self.bounds.size;
    CGRect contentsFrame = self.imageView.frame;
    
    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }
    
    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }
    
    self.imageView.frame = contentsFrame;
}

- (void)resetImage:(UIImage *)image {
    self.imageView.image = image;
    
    if (CGRectEqualToRect(CGRectZero, self.bounds)) {
        return;
    }
    
    if (CGRectEqualToRect(CGRectZero, self.imageView.bounds)) {
        CGRect frame = CGRectZero;
        frame.size = image.size;
        self.imageView.frame = frame;
    }
    self.minimumZoomScale = 1.0f;
    self.maximumZoomScale = 1.0f;
    
    CGRect frame = self.bounds;
    CGFloat minScale = MIN(frame.size.height/image.size.height, frame.size.width/image.size.width);
    self.minimumZoomScale = minScale;
    self.maximumZoomScale = 2.0f;
    self.zoomScale = minScale;
    
    [self centerScrollViewContents];
}

#pragma mark - 协议
#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    [self centerScrollViewContents];
}

#pragma mark MGScrollViewTapDelegate
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    
    // 1
    CGPoint pointInView = [recognizer locationInView:self.imageView];
    
    // 2
    CGFloat newZoomScale = self.zoomScale * 1.5f;
    newZoomScale = MIN(newZoomScale, self.maximumZoomScale);
    if(self.zoomScale==self.maximumZoomScale && newZoomScale == self.maximumZoomScale)
        return;
    
    // 3
    CGSize scrollViewSize = self.bounds.size;
    
    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = pointInView.x - (w / 2.0f);
    CGFloat y = pointInView.y - (h / 2.0f);
    
    CGRect rectToZoomTo = CGRectMake(x, y, w, h);
    
    // 4
    [self zoomToRect:rectToZoomTo animated:YES];
    
}

- (void)scrollViewSingleTapped:(UITapGestureRecognizer*)recognizer {
    
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    CGFloat newZoomScale = self.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, self.minimumZoomScale);
    [self setZoomScale:newZoomScale animated:YES];
}

#pragma mark - 属性
- (void)setImage:(UIImage *)image {
    _image = image;
    [self resetImage:image];
}


@end
